import React, { Component } from "react";
//父传子通过属性传值...
/*
class Parent1 extends Component{
    state={
        data:'碰磕'
    }
    render(){
        return(
            <div>
            <div>我是父组件</div>
            <Child baba={this.state.data}></Child>
            </div>
        )
    }
}*/

/*
class Child extends Component{
    render(){
        return(
            <div>我是子组件:{this.props.baba}</div>
        )
    }
}
*/

//子传父
class Parent extends Component{
    state={
        gift:null
    }
    //回调函数
    callback=(obj)=>{
        console.log(`收到礼物:${obj}`);
        this.setState({
            gift:obj
        })
    }
    render(){
        let {gift} =this.state;
        return(
            <div>
                <h3>我的礼物:{gift}</h3>
                <hr/>
                <Child1 gifts={this.callback}></Child1>
            </div>
        )
    }
}

class Child1 extends Component{
    state={
        gift:'脑白金'
    }
    songgift=()=>{
        this.props.gifts(this.state.gift)   //调用父亲的函数并进行传值
    }
    render(){
        let {gift} =this.state;
        return(
            <div>
                <h3>我是子组件</h3>
                <button onClick={this.songgift}>向父亲送礼</button>
            </div>
        )
    }
}
export default Parent